import { defineStore } from "pinia";
// import { UserState } from "../interface";
import piniaPersistConfig from "../helper/persist";
import { loginApi } from "../../http/index";

export const useUserStore = defineStore("user", {
  state: (): any => ({
    token: uni.getStorageSync("token") || "",
    userInfo: { username: "" },
  }),
  getters: {},
  actions: {
    /**
     * @use 保存token
     */
    setToken(token: string) {
      this.token = token;
      uni.setStorageSync("token", token);
    },

    /**
     * @use 保存用户名密码
     * @param userInfo 用户名
     */
    setUserInfo(userInfo: any["userInfo"]) {
      this.userInfo = userInfo;
    },

    async login(par: any) {
      try {
        const res = await loginApi(par);
        console.log("res", res);
        if (res.code == 200) {
          uni.showToast({
            title: "登录成功",
            icon: "none",
            duration: 2000,
          });
          this.setToken(res.data.token);
        }
      } catch (error) {}
    },
  },
  // persist: piniaPersistConfig(`user`),
});
